package com.wangp.qms.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.wangp.qms.dbcenter.DBCenter;
import com.wangp.qms.entity.User;
import com.wangp.qms.service.IBackendService;
import com.wangp.qms.util.PropertiesUtils;

@Service("iBackendService")
public class BackendServiceImpl implements IBackendService {

	// 当前vip服务号码
	private Integer VIP_NUM = 1;
	// 当前prior服务号码
	private Integer PRIOR_NUM = 1;
	// 当前服务general号码
	private Integer GENERAL_NUM = 1;

	// vip服务台当前服务号码
	private Integer VIP_SERVICE_NUM = 1;

	// PRIOR服务台当前服务号码
	private Integer PRIOR_SERVICE_NUM = 1;

	// GENERA服务台当前服务号码
	private Integer GENERAL_SERVICE_NUM = 1;

	// 是否降级服务
	private boolean isServiceLower = Boolean.valueOf(PropertiesUtils.getValues("backend.service.isServiceLower"));

	// 当前服务中Vip队列
	public static Map<Integer, User> vipUsersQueue = new HashMap<Integer, User>();

	// 当前服务中Vip队列
	public static Map<Integer, User> priorUsersQueue = new HashMap<Integer, User>();

	// 当前服务中Vip队列
	public static Map<Integer, User> generalUsersQueue = new HashMap<Integer, User>();

	public synchronized String vipBackendService() {

		// Integer serviceNum =
		// Integer.valueOf(PropertiesUtils.getValues("backend.vipservice.total"));
		// 当前服务人员是否已满
		// if (vipUsersQueue.size() >= serviceNum) {
		// return "当前窗口已满，请稍后！";
		// } else {// 未满则服务当前排队人员

		User user = DBCenter.getVIP(VIP_NUM);
		
		if (user != null) {// 查询当前vip是否有人排队
			vipUsersQueue.put(VIP_SERVICE_NUM++, user);
			return "开始服务VIP" + (VIP_NUM++) + "号顾客！";
		} else {
			if (isServiceLower) {
				user = DBCenter.getPrior(PRIOR_NUM);
				if (user != null) {
					vipUsersQueue.put(VIP_SERVICE_NUM++, user);
					return "开始服务PRIOR" + (PRIOR_NUM++) + "号顾客！";
				} else {
					user = DBCenter.getGeneral(GENERAL_NUM);
					if (user != null) {
						vipUsersQueue.put(VIP_SERVICE_NUM++, user);
						return "开始服务GENERAL" + (GENERAL_NUM++) + "号顾客！";
					}
				}

			}
			return "当前没有排队顾客！";
		}
	}

	public String priorBackendService() {
//		Integer serviceNum = Integer.valueOf(PropertiesUtils.getValues("backend.priorservice.total"));
//		// 当前服务人员是否已满
//		if (priorUsersQueue.size() >= serviceNum) {
//			return "当前窗口已满，请稍后！";
//		} else {// 未满则服务当前排队人员
		
			User user = DBCenter.getPrior(PRIOR_NUM);
			if (user != null) {
				priorUsersQueue.put(PRIOR_SERVICE_NUM++, user);
				return "开始服务PRIOR" + (PRIOR_NUM++) + "号顾客！";
			} else {
				if (isServiceLower) {
					user = DBCenter.getGeneral(GENERAL_NUM);
					if (user != null) {
						priorUsersQueue.put(PRIOR_SERVICE_NUM++, user);
						return "开始服务GENERAL" + (GENERAL_NUM++) + "号顾客！";
					}
				}
			}
		return "当前没有排队顾客！";
	}

	public String generalBackendService() {
//		Integer serviceNum = Integer.valueOf(PropertiesUtils.getValues("backend.generalservice.total"));
//		// 当前服务人员是否已满
//		if (generalUsersQueue.size() >= serviceNum) {
//			return "当前窗口已满，请稍后！";
//		} else {// 未满则服务当前排队人员
			User user = DBCenter.getGeneral(GENERAL_NUM);
			if (user != null) {
				generalUsersQueue.put(GENERAL_SERVICE_NUM++, user);
				return "开始服务GENERAL" + (GENERAL_NUM++) + "号顾客！";
			}
		return "当前没有排队顾客！";
	}

}
